package cn.mulanbay.job.persistent.service;

import cn.mulanbay.job.web.bean.request.trigger.TaskTriggerCategorySearch;
import cn.mulanbay.persistent.query.QueryBuilder;
import cn.mulanbay.schedule.domain.TaskLog;
import cn.mulanbay.schedule.domain.TaskTrigger;
import cn.mulanbay.schedule.impl.MybatisPersistentProcessor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 调度
 *
 * @author fenghong
 * @create 2022-01-16 10:00
 */
@Service
public class YcScheduleService extends MybatisPersistentProcessor {

    /**
     * 重置调度总执行次数
     *
     * @param id
     */
    public void resetTaskTriggerTotalCount(Long id) {
        TaskTrigger tt = this.selectTaskTrigger(id);
        tt.setTotalCount(0L);
        taskTriggerMapper.updateById(tt);
    }

    /**
     * 重置调度总失败次数
     *
     * @param id
     */
    public void resetTaskTriggerFailCount(Long id) {
        TaskTrigger tt = this.selectTaskTrigger(id);
        tt.setFailCount(0L);
        taskTriggerMapper.updateById(tt);
    }

    /**
     * 更新调度参数
     *
     * @param id
     * @param triggerParas
     */
    public void updateTaskTriggerPara(Long id, String triggerParas) {
        TaskTrigger tt = this.selectTaskTrigger(id);
        tt.setTriggerParas(triggerParas);
        tt.setModifyTime(new Date());
        taskTriggerMapper.updateById(tt);
    }

    /**
     * 获取最近一次的调度日志
     *
     * @param taskTriggerId
     */
    public TaskLog getLastTaskLog(Long taskTriggerId) {
        return taskLogMapper.getLastTaskLog(taskTriggerId);
    }

    /**
     * 获取分类列表，统计聚合
     *
     * @return
     */
    public List<String> getTaskTriggerCategoryList(TaskTriggerCategorySearch sf) {
        QueryWrapper qw = QueryBuilder.build(sf);
        return taskTriggerMapper.getCategoryList(qw,sf.getGroupField());
    }

}
